﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyCommon.Seed;
using MyModels;
using SqlSugar;

namespace MyApi.Controllers
{
    /// <summary>
    /// 从数据库表生成项目整体架构文件类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DbFirstController : ControllerBase
    {
        private readonly SqlSugarScope _sugarClient;
        private readonly IWebHostEnvironment _env;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sugarClient"></param>
        /// <param name="env"></param>
        public DbFirstController(ISqlSugarClient sugarClient,IWebHostEnvironment env)
        {
            _sugarClient = sugarClient as SqlSugarScope;
            _env = env;
        }

        /// <summary>
        /// 创建控制器
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> CreateController([FromBody]string[] tableNames)
        {
            var data = new MessageModel<string>() { success=true,message=""};

            if (_env.IsDevelopment()) {
                var res = FrameGenerate.CreateControllers(_sugarClient, tableNames);
                data.data = $"生成Controller层代码 {res}";

            }
            else
            {
                data.success = false;
                data.message = "当前处于生成模式，代码生成不可用";
            }
            return data;
        }
    }
}
